﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StateController : MonoBehaviour {

    public State currentState;
    public State remainState;
    public Transform eyes;

    [HideInInspector]
    public float stateTimeElapsed;
    [HideInInspector]
    public Transform chaseTarget;
    [HideInInspector]
    public IJoyStick joyStick;
    [HideInInspector]
    public TankShooting tankShooting;
    [HideInInspector]
    public TankMove tankMove;

    private bool _aiActive = true;

	void Awake() {
        joyStick = GetComponentInChildren<IJoyStick>();
        tankShooting = GetComponent<TankShooting>();
        tankMove = GetComponent<TankMove>();
	}
	
	void Update () {
        if (!_aiActive)
            return;
        currentState.UpdateState(this);
    }

    void OnEnable()
    {
    }

    void OnDrawGizmos()
    {
        if (currentState != null && eyes != null)
        {
            Gizmos.color = currentState.sceneGizmosColor;
            Gizmos.DrawWireSphere(eyes.position, 1f);
        }
    }

    public void SetupAI(bool activationFromTankManager)
    {
        _aiActive = activationFromTankManager;
    }

    public void TransitionToState(State nextState)
    {
        //nextState为空则代表不改变当前状态
        if (nextState!=null && nextState != remainState)
        {
            currentState = nextState;
            OnExitState();
        }
    }

    public bool CheckIfCountDownElapsed(float duration)
    {
        stateTimeElapsed += Time.deltaTime;
        return (stateTimeElapsed >= duration);
    }

    private void OnExitState()
    {
        stateTimeElapsed = 0;
    }
}
